package com.dy.basic.service.impl;

import com.dy.basic.mapper.BaseMapper;
import com.dy.basic.query.BaseQuery;
import com.dy.basic.service.BaseService;
import com.dy.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        // 获取查询的分页条数
        Integer totals = baseMapper.queryByCount(query);

        // 如果查询的总条数为空，那么就没有必要再查询数据了
        if (totals == 0){
            return new PageList<>();
        }

        // 获取分页数据
        List<T> rows = baseMapper.queryByPage(query);

        // 返回分页的对象
        return new PageList<T>(totals,rows);

    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteAll(List<Long> ids) {
        baseMapper.deleteAll(ids);
    }
}
